<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>实现防止重复请求 | 爆米花小布</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/favicon.ico">
    <script defer="defer" async="async" src="//cpro.baidustatic.com/cpro/ui/cm.js"></script>
    <meta name="description" content=" ">
    
    <link rel="preload" href="/assets/css/0.styles.54de4a22.css" as="style"><link rel="preload" href="/assets/js/app.36e8cb25.js" as="script"><link rel="preload" href="/assets/js/2.cd8e88cf.js" as="script"><link rel="preload" href="/assets/js/38.6c2de9e4.js" as="script"><link rel="prefetch" href="/assets/js/10.5a175a73.js"><link rel="prefetch" href="/assets/js/11.7c8d5810.js"><link rel="prefetch" href="/assets/js/12.c77aca0d.js"><link rel="prefetch" href="/assets/js/13.fc475cb0.js"><link rel="prefetch" href="/assets/js/14.517ed6e7.js"><link rel="prefetch" href="/assets/js/15.7dc9cc39.js"><link rel="prefetch" href="/assets/js/16.4223237a.js"><link rel="prefetch" href="/assets/js/17.0e3dcd26.js"><link rel="prefetch" href="/assets/js/18.132512c7.js"><link rel="prefetch" href="/assets/js/19.55275db3.js"><link rel="prefetch" href="/assets/js/20.621b52a1.js"><link rel="prefetch" href="/assets/js/21.daa66e46.js"><link rel="prefetch" href="/assets/js/22.537fd259.js"><link rel="prefetch" href="/assets/js/23.81c79725.js"><link rel="prefetch" href="/assets/js/24.ee80a08c.js"><link rel="prefetch" href="/assets/js/25.260c7616.js"><link rel="prefetch" href="/assets/js/26.03e10148.js"><link rel="prefetch" href="/assets/js/27.768e5af1.js"><link rel="prefetch" href="/assets/js/28.ad0f08eb.js"><link rel="prefetch" href="/assets/js/29.9e188b97.js"><link rel="prefetch" href="/assets/js/3.9cba588d.js"><link rel="prefetch" href="/assets/js/30.f2aa9a7c.js"><link rel="prefetch" href="/assets/js/31.93c74095.js"><link rel="prefetch" href="/assets/js/32.4735eb89.js"><link rel="prefetch" href="/assets/js/33.7b05518b.js"><link rel="prefetch" href="/assets/js/34.3db00b39.js"><link rel="prefetch" href="/assets/js/35.ea9a0227.js"><link rel="prefetch" href="/assets/js/36.5873bde7.js"><link rel="prefetch" href="/assets/js/37.2cbd5350.js"><link rel="prefetch" href="/assets/js/39.d3c0116f.js"><link rel="prefetch" href="/assets/js/4.97aec80c.js"><link rel="prefetch" href="/assets/js/40.47b05f9f.js"><link rel="prefetch" href="/assets/js/41.5dfb852e.js"><link rel="prefetch" href="/assets/js/42.1d7cf4c2.js"><link rel="prefetch" href="/assets/js/43.43f59d94.js"><link rel="prefetch" href="/assets/js/44.19309bf6.js"><link rel="prefetch" href="/assets/js/45.dbee3f0a.js"><link rel="prefetch" href="/assets/js/46.a373745d.js"><link rel="prefetch" href="/assets/js/5.bc4ebf1d.js"><link rel="prefetch" href="/assets/js/6.805bb5f3.js"><link rel="prefetch" href="/assets/js/7.9724c5a3.js"><link rel="prefetch" href="/assets/js/8.6c6d5662.js"><link rel="prefetch" href="/assets/js/9.641efeda.js">
    <link rel="stylesheet" href="/assets/css/0.styles.54de4a22.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">爆米花小布</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/element-ui/el-table-select-all/" class="nav-link">
  前端博客
</a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-element-dict/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  PC端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-vant-dict" target="_blank" rel="noopener noreferrer" class="nav-link external">
  移动端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/element-ui/el-table-select-all/" class="nav-link">
  前端博客
</a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-element-dict/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  PC端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-vant-dict" target="_blank" rel="noopener noreferrer" class="nav-link external">
  移动端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Element-ui组件使用经验</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/element-ui/el-table-select-all/" class="sidebar-link">使用el-table实现全选功能</a></li><li><a href="/element-ui/el-table-sku/" class="sidebar-link">利用element实现sku规格相关功能</a></li><li><a href="/element-ui/el-cropper/" class="sidebar-link">利用vue-cropper实现图片裁剪功能</a></li><li><a href="/element-ui/el-form-nesting/" class="sidebar-link">el-form嵌套表单校验</a></li><li><a href="/element-ui/el-theme/" class="sidebar-link">使用element-ui组件库实现主题切换功能</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>JS必备知识</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/JS/call-apply-bind/" class="sidebar-link">js中的call、apply和bind</a></li><li><a href="/JS/debounce-throttle/" class="sidebar-link">JS中防抖与节流的讲解与封装</a></li><li><a href="/JS/tree-data/" class="sidebar-link">JS中树形结构数据相关方法</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>CSS实战经验</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/CSS/contour-layout/" class="sidebar-link">css实现等高布局</a></li><li><a href="/CSS/fixed-aspect-ratio/" class="sidebar-link">css实现固定宽高比</a></li><li><a href="/CSS/draw-triangle/" class="sidebar-link">CSS画一个三角形</a></li><li><a href="/CSS/draw-ring/" class="sidebar-link">CSS画一个环形进度条</a></li><li><a href="/CSS/pseudo-class/" class="sidebar-link">伪类和伪元素的区别与使用</a></li><li><a href="/CSS/clear-float/" class="sidebar-link">清除浮动的原理与时机</a></li><li><a href="/CSS/layout/" class="sidebar-link">三栏布局问题（左右固定宽度 中间自适应）</a></li><li><a href="/CSS/flex/" class="sidebar-link">flex布局祥解</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>实用的npm包</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/npm/xiaobu-pic-zoom/" class="sidebar-link">xiaobu-pic-zoom图片放大镜</a></li><li><a href="/npm/vue-message-code/" class="sidebar-link">vue-message-code短信验证码</a></li><li><a href="/npm/xiaobu-water-marker/" class="sidebar-link">xiaobu-water-marker水印</a></li><li><a href="/npm/vue-flutter/" class="sidebar-link">vue-flutter漂浮动画</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>VUE小妙招</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue-config/search-menu/" class="sidebar-link">利用fuse.js实现搜索菜单功能</a></li><li><a href="/vue-config/alias/" class="sidebar-link">vue项目配置别名</a></li><li><a href="/vue-config/svg-sprite-loader/" class="sidebar-link">vue项目配置使用svg图标</a></li><li><a href="/vue-config/vue-mode-env/" class="sidebar-link">vue项目的模式和环境变量</a></li><li><a href="/vue-config/system-config/" class="sidebar-link">vue项目配置系统变量</a></li><li><a href="/vue-config/axios-optimization-one/" class="sidebar-link">vue项目axios请求优化（一）</a></li><li><a href="/vue-config/axios-optimization-two/" class="sidebar-link">vue项目axios请求优化（二）</a></li><li><a href="/vue-config/axios-optimization-three/" aria-current="page" class="active sidebar-link">vue项目axios请求优化（三）</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/vue-config/axios-optimization-three/#总结" class="sidebar-link">总结</a></li><li class="sidebar-sub-header"><a href="/vue-config/axios-optimization-three/#好npm包推荐" class="sidebar-link">好NPM包推荐</a></li><li class="sidebar-sub-header"><a href="/vue-config/axios-optimization-three/#广告" class="sidebar-link">广告</a></li></ul></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>前端工具</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/tool/use-nvm/" class="sidebar-link">nvm的使用</a></li><li><a href="/tool/vscode/sftp/" class="sidebar-link">vscode插件之sftp</a></li><li><a href="/tool/vscode/koroFileHeader/" class="sidebar-link">vscode插件之koroFileHeader</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="实现防止重复请求"><a href="#实现防止重复请求" class="header-anchor">#</a> 实现防止重复请求</h1> <p>​		对于测试人员来说，重复点击按钮进行测试的这个动作怎一个熟练了得，如果没对按钮做防止重复请求的操作，测试人员将喜提一个bug并内心深处肯定是。。。（好菜）所以对按钮进行防止重复请求操作非常重要。</p> <h3 id="黄铜"><a href="#黄铜" class="header-anchor">#</a> 黄铜</h3> <p>​		添加一个变量，判断是否点击了按钮，是的话再次点击直接return,否则触发请求</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>isLoading<span class="token punctuation">)</span> <span class="token keyword">return</span>
isLoading <span class="token operator">=</span> <span class="token boolean">true</span>
XXXApi<span class="token punctuation">.</span><span class="token constant">XXX</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">data</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
	<span class="token operator">...</span><span class="token operator">...</span>
    isLoading <span class="token operator">=</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token parameter">err</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    isLoading <span class="token operator">=</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="白银"><a href="#白银" class="header-anchor">#</a> 白银</h3> <p>​		给按钮添加loading状态遮罩层，如el-button 添加 loading属性。多了loading动画，免了判断。</p> <div class="language-js extra-class"><pre class="language-js"><code> <span class="token operator">&lt;</span>el<span class="token operator">-</span>button <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;two-words&quot;</span> type<span class="token operator">=</span><span class="token string">&quot;primary&quot;</span> <span class="token operator">:</span>loading<span class="token operator">=</span><span class="token string">&quot;isLoading&quot;</span> @click<span class="token operator">=</span><span class="token string">&quot;handleQuery&quot;</span><span class="token operator">&gt;</span>查询<span class="token operator">&lt;</span><span class="token operator">/</span>el<span class="token operator">-</span>button<span class="token operator">&gt;</span>

isLoading <span class="token operator">=</span> <span class="token boolean">true</span>
XXXApi<span class="token punctuation">.</span><span class="token constant">XXX</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">data</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
	<span class="token operator">...</span><span class="token operator">...</span>
    isLoading <span class="token operator">=</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token parameter">err</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    isLoading <span class="token operator">=</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="铂金"><a href="#铂金" class="header-anchor">#</a> 铂金</h3> <p>​		从根源处解决防止重复请求，当请求未响应时，遇到相同请求不予操作。</p> <p>​		<a href="https://mp.weixin.qq.com/s/pj6d1qxWa4CPoHV0FK7bzA" target="_blank" rel="noopener noreferrer">VUE项目利用Axios实现好用的API层请求接口封装<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="钻石"><a href="#钻石" class="header-anchor">#</a> 钻石</h3> <p>​		相对铂金的方式，解决了铂金的方式存在当响应较慢时，更改数据后再点击按钮后无做任何操作，导致提交的数据并非最新更改后的数据。因此还需要优化。</p> <p>​		使用axios取消请求的方式，实现对相同请求，取消前面的请求。</p> <p>上代码</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// request.js</span>

<span class="token keyword">import</span> axios <span class="token keyword">from</span> <span class="token string">&quot;@/api/axios&quot;</span>

<span class="token keyword">import</span> originalAxios <span class="token keyword">from</span> <span class="token string">&quot;axios&quot;</span>

<span class="token keyword">const</span> <span class="token constant">GLOBAL_REQUEST_OBJ</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">newRequest</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> params</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">GLOBAL_REQUEST_OBJ</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token constant">GLOBAL_REQUEST_OBJ</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">cancel</span><span class="token punctuation">(</span>params<span class="token punctuation">.</span>showLoading<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">const</span> CancelToken <span class="token operator">=</span> originalAxios<span class="token punctuation">.</span>CancelToken
    <span class="token constant">GLOBAL_REQUEST_OBJ</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span> <span class="token operator">=</span> CancelToken<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span>params<span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">cancelToken</span><span class="token operator">:</span> <span class="token constant">GLOBAL_REQUEST_OBJ</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">.</span>token<span class="token punctuation">}</span><span class="token punctuation">)</span>

    <span class="token function">axios</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">result</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">delete</span> <span class="token constant">GLOBAL_REQUEST_OBJ</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span>
      <span class="token function">resolve</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token parameter">err</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token function">reject</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token punctuation">[</span><span class="token string">&quot;get&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;post&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;delete&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;put&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;patch&quot;</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">type</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  newRequest<span class="token punctuation">[</span>type<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> <span class="token operator">...</span>params</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> isShowLoading <span class="token operator">=</span> <span class="token boolean">false</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>params<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>params <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span>type <span class="token operator">===</span> <span class="token string">&quot;get&quot;</span> <span class="token operator">||</span> type <span class="token operator">===</span> <span class="token string">&quot;delete&quot;</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>params<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      isShowLoading <span class="token operator">=</span> params<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>showLoading <span class="token operator">||</span> <span class="token boolean">false</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      isShowLoading <span class="token operator">=</span> params<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span>showLoading <span class="token operator">||</span> <span class="token boolean">false</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">GLOBAL_REQUEST_OBJ</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token constant">GLOBAL_REQUEST_OBJ</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">cancel</span><span class="token punctuation">(</span>isShowLoading<span class="token punctuation">)</span>
      <span class="token punctuation">}</span>

      <span class="token keyword">const</span> CancelToken <span class="token operator">=</span> originalAxios<span class="token punctuation">.</span>CancelToken
      <span class="token constant">GLOBAL_REQUEST_OBJ</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span> <span class="token operator">=</span> CancelToken<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
      <span class="token comment">// 第二个参数对象 存在 params且是 get 请求 或者 delete 请求， 无第三个参数 config</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>params<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>params <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span>type <span class="token operator">===</span> <span class="token string">&quot;get&quot;</span> <span class="token operator">||</span> type <span class="token operator">===</span> <span class="token string">&quot;delete&quot;</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>params<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        params<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>cancelToken <span class="token operator">=</span> <span class="token constant">GLOBAL_REQUEST_OBJ</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">.</span>token
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// 配置config</span>
        Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span>params<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">cancelToken</span><span class="token operator">:</span> <span class="token constant">GLOBAL_REQUEST_OBJ</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">.</span>token<span class="token punctuation">}</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
      axios<span class="token punctuation">[</span>type<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> params<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">result</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">delete</span> <span class="token constant">GLOBAL_REQUEST_OBJ</span><span class="token punctuation">[</span>name<span class="token punctuation">]</span>
        <span class="token function">resolve</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token parameter">err</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token function">reject</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">const</span> axiosRequest <span class="token operator">=</span> newRequest
</code></pre></div><p>接口请求的地方使用</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// common-api.js</span>

<span class="token keyword">import</span> <span class="token punctuation">{</span> axiosRequest <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;@/api/request&quot;</span>
<span class="token keyword">import</span> axios <span class="token keyword">from</span> <span class="token string">&quot;@/api/axios&quot;</span>

<span class="token comment">/**
 * @description: get请求示例
 * @param {*} data
 * @param {*} config
 * @return {*}
 * @author: syx
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">getDemo</span><span class="token punctuation">(</span><span class="token parameter">data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> config <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> options <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">params</span><span class="token operator">:</span> data <span class="token punctuation">}</span><span class="token punctuation">,</span> config<span class="token punctuation">)</span>
  <span class="token keyword">return</span> axiosRequest<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;commmonApi&quot;</span> <span class="token operator">+</span> <span class="token string">&quot;getDemo&quot;</span><span class="token punctuation">,</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">/url</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span> options<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * @description: post请求示例
 * @param {*}
 * @return {*}
 * @author: syx
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">postDemo</span><span class="token punctuation">(</span><span class="token parameter">data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> config <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> axiosRequest<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">&quot;commmonApi&quot;</span> <span class="token operator">+</span> <span class="token string">&quot;postDemo&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;url&quot;</span><span class="token punctuation">,</span> data<span class="token punctuation">,</span> config<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * @description: delete请求示例1
 * @param {*}
 * @return {*}
 * @author: syx
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">deleteDemo1</span><span class="token punctuation">(</span><span class="token parameter">data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> config <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> axiosRequest<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string">&quot;commmonApi&quot;</span> <span class="token operator">+</span> <span class="token string">&quot;deleteDemo1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;url&quot;</span><span class="token punctuation">,</span> data<span class="token punctuation">,</span> config<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * @description:  delete请求示例2
 * @param {*} data
 * @param {*} config
 * @return {*}
 * @author: syx
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">deleteDemo2</span><span class="token punctuation">(</span><span class="token parameter">data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> config <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> options <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">params</span><span class="token operator">:</span> data <span class="token punctuation">}</span><span class="token punctuation">,</span> config<span class="token punctuation">)</span>
  <span class="token keyword">return</span> axiosRequest<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string">&quot;commmonApi&quot;</span> <span class="token operator">+</span> <span class="token string">&quot;deleteDemo2&quot;</span><span class="token punctuation">,</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">/url</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span> options<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * @description: put请求示例
 * @param {*}
 * @return {*}
 * @author: syx
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">putDemo</span><span class="token punctuation">(</span><span class="token parameter">data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> config <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> axiosRequest<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;commmonApi&quot;</span> <span class="token operator">+</span> <span class="token string">&quot;putDemo&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;url&quot;</span><span class="token punctuation">,</span> data<span class="token punctuation">,</span> config<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * @description: patch请求示例
 * @param {*}
 * @return {*}
 * @author: syx
 */</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">patchDemo</span><span class="token punctuation">(</span><span class="token parameter">data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> config <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> axiosRequest<span class="token punctuation">.</span><span class="token function">patch</span><span class="token punctuation">(</span><span class="token string">&quot;commmonApi&quot;</span> <span class="token operator">+</span> <span class="token string">&quot;patchDemo&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;url&quot;</span><span class="token punctuation">,</span> data<span class="token punctuation">,</span> config<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="总结"><a href="#总结" class="header-anchor">#</a> 总结</h2> <p>​	对于黄铜和白银的方式，存在很多按钮时，需要定义多个变量，且项目中处处需要添加代码。</p> <p>​	铂金和钻石则采用从根源处解决问题，无需添加代码判断，钻石解决了铂金存在的问题，如有更好的方式，欢迎指导。</p> <p>​	此篇文章为axios请求优化其三，关于防止接口重复请求的优化，关注微信公众号【爆米花小布】不迷路。如有问题，可私信。</p> <h2 id="好npm包推荐"><a href="#好npm包推荐" class="header-anchor">#</a> 好NPM包推荐</h2> <ol><li><p><a href="https://www.npmjs.com/package/vue-element-dict" target="_blank" rel="noopener noreferrer">vue-element-dict<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>  基于element组件库的vue项目轻松使用字典组件，一行代码解决字典问题。</p> <p>自带字典，时间，日期以及脱敏等过滤器及方法</p> <p>https://www.npmjs.com/package/vue-element-dict</p></li> <li><p><a href="https://www.npmjs.com/package/vue-vant-dict" target="_blank" rel="noopener noreferrer">vue-vant-dict<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>         基于vant组件库的vue项目轻松使用字典组件，一行代码解决字典问题。</p> <p>自带字典，时间，日期以及脱敏等过滤器及方法</p> <p>https://www.npmjs.com/package/vue-vant-dict</p></li></ol> <h2 id="广告"><a href="#广告" class="header-anchor">#</a> 广告</h2> <p>此处可接受广告</p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/vue-config/axios-optimization-two/" class="prev">
        vue项目axios请求优化（二）
      </a></span> <span class="next"><a href="/tool/use-nvm/">
        nvm的使用
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><div id="loading-mask"><div class="loading-wrapper"><span class="loading-dot loading-dot-spin"><i></i> <i></i> <i></i> <i></i></span></div></div><!----></div></div>
    <script src="/assets/js/app.36e8cb25.js" defer></script><script src="/assets/js/2.cd8e88cf.js" defer></script><script src="/assets/js/38.6c2de9e4.js" defer></script>
  </body>
</html>
